package dao;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.TypedQuery;
import model.File;
import model.PrimaryKeyShare;
import model.Share;
import model.ShareUser;

public class ShareDAO extends DAO<Share> {

	private List<Share> listShare = new ArrayList<>();

	public ShareDAO() {
		super();
		listShare = getAllShare();
	}

	public List<Share> getListShare() {
		return listShare;
	}

	public void setListShare(List<Share> listePartage) {
		this.listShare = listePartage;
	}

	/*
	 * Cette methode permet d'ajouter un partage à la base
	 */
	public void addShare(int idShareOnPhone, ShareUser sender,
			String nomPartage, List<ShareUser> receiver, String typeFile,
			String pathFile) {

		EntityManager em = DAO.getEm();
		EntityTransaction t = em.getTransaction();

		if (t.isActive()) {
			System.out.println("active");
			t.rollback();
		}

		Share share = new Share();
		share.setIdSender(sender.getIdUser());
		share.setIdShareOnPhone(idShareOnPhone);
		share.setSender(sender);
		share.setReceiver(receiver);
		share.setNameShare(nomPartage);
		share.setDateCreationShare(java.util.Calendar.getInstance()
				.getTimeInMillis());
		share.setDateLastModificationShare(java.util.Calendar.getInstance()
				.getTimeInMillis());

		model.File f = new File();
		f.setShare(share);
		f.setTypeFile(typeFile);
		f.setPathFile(pathFile);

		share.setFile(f);
		sender.addSentShare(share);

		t.begin();
		
		for (ShareUser su : receiver) {
			su.addReceiveShare(share);
		}

		em.persist(share);
		em.persist(f);
		
		t.commit();

	}

	/*
	 * cette méthode permet de recupérer un partage particulier
	 */
	public Share getShareById(int idShare, int idUser) {

		EntityManager em = DAO.getEm();
		EntityTransaction t = em.getTransaction();

		if (t.isActive()) {
			t.rollback();
		}

		t.begin();

		Share share = null;

		PrimaryKeyShare pks = new PrimaryKeyShare(idShare, idUser);
		share = em.find(Share.class, pks);

		return share;
	}

	/*
	 * cette méthode permet de lister tous les partage
	 */
	public List<Share> getAllShare() {

		List<Share> listPartage = new ArrayList<Share>();

		EntityManager em = DAO.getEm();

		String query = "SELECT p FROM Share p ";

		TypedQuery<Share> requete = (TypedQuery<Share>) em.createQuery(query,
				Share.class);
		listPartage = requete.getResultList();

		return listPartage;
	}
	
	/*
	 * cette méthode permet de lister tous les partage recu d'un  utilisateur
	 */
	public List<Share> getAllReceivedShare(int idUser, long lastModification) {

		List<Share> listPartage = null;

		EntityManager em = DAO.getEm();
		
		ShareUser s = em.find(ShareUser.class, idUser);
		listPartage=s.getReceive();

		return listPartage;
	}

	/*
	 * Cette méthode permet de supprimer un partage
	 */
	public void deleteShare(int idShareOnPhone, int idSender) {

		EntityManager em = DAO.getEm();
		EntityTransaction t = em.getTransaction();

		if (t.isActive()) {
			t.rollback();
		}

		t.begin();
		
		PrimaryKeyShare pks = new PrimaryKeyShare(idShareOnPhone, idSender);
		Share share = em.find(Share.class, pks);
		System.out.println(share.getNameShare());

		ShareUser su = em.find(ShareUser.class, idSender);
		su.removeSentShare(share);

		for (ShareUser shareUser : share.getReceiver()) {
			shareUser.removeReceiveShare(share);
		}

		em.remove(share);

		t.commit();

	}

	/*
	 * Cette methode permet de modifier un utilisateur à la base
	 */
	public void changeShare(int idSender, int idShareOnPhone, String newName) {

		EntityManager em = DAO.getEm();
		EntityTransaction t = em.getTransaction();

		if (t.isActive()) {
			t.rollback();
		}

		Share share;
		PrimaryKeyShare pkshare = new PrimaryKeyShare();
		pkshare.setIdSender(idSender);
		pkshare.setIdShareOnPhone(idShareOnPhone);
		share = em.find(Share.class, pkshare);
		share.setNameShare(newName);

		t.begin();
		em.merge(share);
		t.commit();
	}

}
